package org.ailoli.aixiang.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.GridView
import android.widget.LinearLayout
import org.ailoli.aixiang.R
import org.ailoli.aixiang.bean.BatteryBean
import org.ailoli.aixiang.data.BatteryData
import org.ailoli.aixiang.widgets.BatteryView
import java.lang.Exception
import java.lang.NullPointerException

class BatteryAdapter : BaseAdapter {

    private var context: Context
    private var dataList: ArrayList<BatteryBean>
    private var mGridView: GridView? = null

    constructor(context: Context, list: ArrayList<BatteryBean>) {
        this.dataList = list
        this.context = context
    }

    internal class BatteryViewHolder {
        var batteryView: BatteryView? = null
        var batteryLayout: LinearLayout? = null
    }

    public fun setDataList(dataList: ArrayList<BatteryBean>) {
        this.dataList = dataList
        notifyDataSetChanged()
    }

    override fun getCount(): Int {
        return this.dataList.size
    }

    override fun getItem(position: Int): Any? {
        return this.dataList.get(position).batteryData
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    public fun setGridView(gridView: GridView) {
        this.mGridView = gridView
    }

    public fun updateOne(index: Int, batteryData: BatteryData) {
        if (mGridView == null) return
        try {
            if (index <= (mGridView!!.childCount - 1)) {
                val view: View = mGridView!!.getChildAt(index)
                val batteryView = view.findViewById<BatteryView>(R.id.batteryView)
                batteryView.setBatteryStatus(batteryData.batteryStatus)
                batteryView.setBatterySoc(batteryData.batterySoc)
            }
        } catch (e: Exception) {
            throw e
        }

    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var holder: BatteryViewHolder? = null
        var cv: View? = convertView
        if (convertView == null) {
            holder = BatteryViewHolder()
            cv = LayoutInflater.from(context).inflate(R.layout.battery_item, parent, false)
            holder.batteryView = cv.findViewById(R.id.batteryView)
            holder.batteryLayout = cv.findViewById(R.id.batteryLayout)
//            holder.batteryView!!.setBatteryStatus(true)
            val gridView = parent!!.findViewById<GridView>(R.id.batteryGridView)
            val height = gridView.height
            val layoutParams: ViewGroup.LayoutParams = holder.batteryLayout!!.layoutParams
            layoutParams.height = height / 3
            holder.batteryLayout!!.setLayoutParams(layoutParams)
            if (dataList.get(position).batteryData == null) {
                holder.batteryView!!.setBatterySoc(0)
                holder.batteryView!!.setBatteryStatus(false)
            } else {
                holder.batteryView!!.setBatterySoc(dataList.get(position).batteryData!!.batterySoc)
                holder.batteryView!!.setBatteryStatus(dataList.get(position).batteryData!!.batteryStatus)
            }
            cv.setTag(cv)
        } else {
//            Log.e("BatteryAdapter1", position.toString())
            holder = convertView.getTag(position) as BatteryViewHolder?
        }
        return cv!!
    }

//    override fun getView(position: Int, convertView: View, parent: ViewGroup): View? {
//        var holder: BatteryViewHolder? = null
//        var cv: View? = null
//        if (convertView == null) {
//            holder = BatteryViewHolder()
//            cv = LayoutInflater.from(parent!!.context).inflate(R.layout.battery_item, parent, false)
//            holder.batteryView = cv.findViewById(R.id.batteryView)
//            holder.batteryView!!.setBatteryStatus(false)
//            cv.setTag(cv)
//        } else {
//            holder = convertView.getTag(position) as BatteryViewHolder?
//        }
//        return cv
//    }

}
